package com.glela.common.verify;

import java.util.HashSet;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import com.glela.common.exception.BusinessError;

/**
 * @author liujie
 *
 * 数据验证器
 */
public class ValidatorUtil {
	private static class ValidatorHolder{
		static Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); 
	}
	
	private ValidatorUtil(){};
	
	public static Set<BusinessError> validate(Object obj){
		Set<ConstraintViolation<Object>> constraintViolations = ValidatorHolder.validator.validate(obj);
		Set<BusinessError> errorSet = new HashSet<>();
		for(@SuppressWarnings("rawtypes") ConstraintViolation err : constraintViolations) {  
			BusinessError e = BusinessError.valueByCode(err.getMessage());
			errorSet.add(e);
		}  
		
		return errorSet;
	}
}
